package net.alche.sdd.servlet.imp;

import java.util.HashMap;
import java.util.Map;

public class SddHttpSession {
  long timeMillis = 0;
  public Map<String, Object> mp = null;

  public SddHttpSession() {
    timeMillis = System.currentTimeMillis();
    mp = new HashMap<>();
  }

  private static Map<String, SddHttpSession> sessionMap = null;

  public void refreshTimes() {
    timeMillis = System.currentTimeMillis();
  }

  public static SddHttpSession getSession(String key) {
    if (sessionMap == null) {
      sessionMap = new HashMap<>();
    }
    SddHttpSession st = sessionMap.get(key);
    if (st != null && st.isEnable()) {
      st.refreshTimes();
    }
    return st;
  }

  public static SddHttpSession putSession(String key, SddHttpSession session) {
    if (sessionMap == null) {
      sessionMap = new HashMap<>();
    }
    return sessionMap.put(key, session);
  }

  public boolean isEnable() {
    return timeMillis >= 0;
  }

  public void invalidate() {
    timeMillis = -1;
  }
}
